import {NegWalker, PosWalker} from "./walker.mjs";
import {Boundary} from "../map/boundary.mjs";

export class Player {
    #keys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight']

    constructor(peeker) {
        this.pos = peeker.pos

        this.boundary = new Boundary(peeker.mapSize)

        this.negWalker = new NegWalker(peeker)
        this.posWalker = new PosWalker(peeker)
    }

    setEventListener() {
        let nextPos = this.nextPos.bind(this)
        window.addEventListener('keydown', nextPos)
    }

    nextPos(event) {
        let key = event.key

        if (!this.#keys.includes(key) || this.boundary.on(key))
            return

        let walker
        switch (key) {
            case 'ArrowUp':
            case 'ArrowLeft':
                walker = this.negWalker
                break
            case 'ArrowDown':
            case 'ArrowRight':
                walker = this.posWalker
                break
        }

        walker.reset(key)
        walker.go(this.pos)
        this.boundary.reset(this.pos)
    }
}
